﻿
public class FSMMachine : IFSMMachine
{
	private IState defState;
	private IState curState;

	private object args;

	public FSMMachine(IState def)
	{
		curState = defState = def;
	}


	public void CheckStateDone(object args = null)
	{
		if (curState != null)
		{
			if (curState.IsComplete())
			{
				ToDefaultState();
			}
		}
	}

	public void ProccessState(IState state, object args = null)
	{
		if (state != null)
		{
			if (curState.Priority < state.Priority)										//优先级高，直接转换
			{
				curState.OnExit(args);
				curState = state;
				curState.OnEnter(args);
			}
			else if(!curState.IsDoing)		 											//当前状态已完成
			{
				curState.OnExit(args);
				curState = state;
				curState.OnEnter(args);
			}
		}
	}

	public void ToDefaultState()
	{
		if (curState != null)
		{
			curState.IsDoing = false;
		}
		ProccessState(defState);
	}
}
